﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ElectroSystem.Models;
using System.ServiceModel;
using TokenGenerator;
using System.Windows.Forms;

namespace ElectroSystem.Controllers
{
    public class RegisterController : MenusController
    {
        //
        // GET: /Register/

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel data)
        {
            if ((new UsersServ.UsersServiceClient().DoesUserNameExist(data.Username)) == true)
            {
                ViewBag.Message = "Username already exists"; 
                return View(); 
            }
            else if ((new UsersServ.UsersServiceClient().DoesEmailExist(data.Email)) == true)
            {
                ViewBag.Message = "Email already exists"; 
                return View(); 
            }
            else if ((new UsersServ.UsersServiceClient().DoesPinExist(data.PinNumber)) == true)
            {
                ViewBag.Message = "Pin already exists";
                return View();
            }
            else
            {
                ViewBag.Message = "";
                new UsersServ.UsersServiceClient().AddUser(data.Username, data.Password, data.Name, data.Surname,
                    data.Email, data.MobileNo, data.Residence, data.Street, data.Town, data.Country, data.PinNumber);
                try
                {
                    openForm();
                }
                catch (Exception ex)
                {
                    ViewBag.Message = ex.Message.ToString();
                }
                return RedirectToAction("Login", "Login");
            }
        }

        public void openForm()
        {
            Form1 f1 = new Form1();
            f1.ShowDialog();
        }
    }
}
